package com.murfrosoft.airhockey.entities;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

public class CompPaddleLevelBase extends CompPaddle {
	
	private float xspeed=1f;
	private float yspeed=1f;
	private float accuracy=.001f;
	
	
	
	@Override
	public void move(int width,int height, float puckX, float puckY) {
		/* UPDATE COMPUTER PADDLE POSITION */
		/* NOTE: When updating paddle position, we must ALSO update it's velocity, since 
		 * this is used in puck collision algorithm!  We will override any default set 
		 * velocities here */
		//accuracyFuzz();
		if( puckY < height/2 ){
			intercept(puckY,height,puckX,width);
		}
		else{
           defense(puckX,width);
		}

		
	}
	
	public float getXspeed() {
		return xspeed;
	}

	public void setXspeed(float xspeed) {
		this.xspeed = xspeed;
	}

	public float getYspeed() {
		return yspeed;
	}

	public void setYspeed(float yspeed) {
		this.yspeed = yspeed;
	}

	public float getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(float accuracy) {
		this.accuracy = accuracy;
	}

	//behavior when puck not on comp's side
	public void defense(float puckX,float width){
		if( positionY > 1.1f*radius ){
			positionY -= 4*yspeed;
			velocityY = -4*yspeed;
		}
		trackX(puckX,width);
	}
	
	//behavior when puck on comp's side
	public void intercept(float puckY,float height,float puckX,float width){
		// Puck on our side of table: try to stay behind it (to avoid puck getting stuck in paddle)
		if( (positionY < (puckY-10)) && (positionY < (height/2 - radius)) ){
			positionY += 3*yspeed;
			velocityY = 3*yspeed;
		}
		else{
			if( positionY > 1.1f*radius ){
				positionY -= 2*yspeed;
				velocityY = -2*yspeed;
			}
		}
		trackX(puckX,width);
	
	}
	
	
	
	public void trackX(float puckX,float width){
		/* If we are left or right of puck, we want to try to stay behind it */
		if( (positionX < (puckX-10)) && (positionX < (width-radius)) ){
			positionX += xspeed;
			velocityX = xspeed;
		}
		else if( (positionX > (puckX+10)) && (positionX > (radius)) ){
			positionX -= xspeed;
			velocityX = -xspeed;
		}
		else{
			velocityX = 0;		// New anti-stutter technology! MCM
		}
	}
	
	
	
	
	
	//makes comp's response less perfect does not work yet
	public void accuracyFuzz(){
		int flip =coinFlip();
			if(flip==1){
				xspeed-=accuracy;
				yspeed+=accuracy;
				Log.d("inf","0");
			}
			else{
				Log.d("inf","1");
				xspeed+=accuracy;
				yspeed-=accuracy;
			}
		
	}
	
	public int coinFlip(){
		Random r = new Random(2);
		int i =r.nextInt();
		return i;
		
	}
	
	

	@Override
	public void draw(Canvas canvas) {
		/* DRAW COMPUTER PADDLE */
		Paint paddle_color3 = new Paint();
		paddle_color3.setColor(Color.rgb(0, 0, 255));
		Paint paddle_color4 = new Paint();
		paddle_color4.setColor(Color.rgb(100, 100, 255));
		paddle_color4.setAntiAlias(true);
		paddle_color3.setAntiAlias(true);
	
		canvas.drawCircle(positionX,positionY, radius, paddle_color3);
		canvas.drawCircle(positionX,positionY, radius/2, paddle_color4);
		//canvas.drawBitmap(bitmap, positionX-radius, positionY-radius, paddle_color4);
		
	}

}
